package com.translate.web.api.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.NativeWebRequest;

import com.basic.service.contract.IBaseService;
import com.controller.base.AbstractCommonController;
import com.param.impl.IdPageParam;
import com.response.vo.ResponseModel;
import com.translate.entity.CommissionManagement;
import com.translate.vo.CommissionManagementVO;
import com.translate.web.param.CommissionManagementAddParam;
import com.translate.web.param.CommissionManagementUpdateParam;
import com.translate.web.param.WithdrawalAddParam;
import com.translate.web.service.ICommissionManagementDetailService;
import com.translate.web.service.ICommissionManagementService;
import com.translate.web.service.IUserService;

@RestController
@RequestMapping("commissionManagement")
public class CommissionManagementController extends AbstractCommonController<CommissionManagement, IdPageParam> {
	private ICommissionManagementService service;
	
	ICommissionManagementDetailService commissionManagementDetailService;

	private IUserService userService;
	
	@Resource
	public void setCommissionManagementDetailService(ICommissionManagementDetailService commissionManagementDetailService) {
		this.commissionManagementDetailService = commissionManagementDetailService;
	}

	public CommissionManagementController(ICommissionManagementService service) {
		this.service = service;
	}
	
	@PostMapping("getDataByUserId")
	public ResponseModel<CommissionManagementVO> getDataByUserId(NativeWebRequest request) {
		long userId = this.getUserId(request);
		
		CommissionManagementVO dataByUserId = this.service.getDataByUserId(userId, CommissionManagementVO.class);
		
		return ResponseModel.<CommissionManagementVO>success(dataByUserId);
	}
	
	/**
	 * 兑换学术币
	 * @param parameter
	 * @return
	 */
	@PostMapping("exchangeAcademicCoins")
	@Transactional
	public ResponseModel<Integer> exchangeAcademicCoins(
			@RequestBody @Validated WithdrawalAddParam parameter,
			NativeWebRequest request
	) {
		long userId = this.getUserId(request);
		
		parameter.setUserId(userId);
		
		int exchangeAcademicCoins = this.service.exchangeAcademicCoins(parameter);
		
		userService.exchangeAcademicCoins(parameter);
		
		commissionManagementDetailService.addCommission(exchangeAcademicCoins, parameter, (byte) 0, "兑换学术币");
		
		return ResponseModel.<Integer>success(exchangeAcademicCoins);
	}
	
	/**
	 * 添加
	 * @param <V>
	 * @param param
	 * @return
	 */
	@PostMapping("/add")
	public ResponseModel<Integer> insert(@RequestBody @Validated CommissionManagementAddParam param,
			HttpServletRequest httpServletRequest) {
		int id = this.service.parseInsert(param, CommissionManagement.class);

		return ResponseModel.<Integer>success(id);
	}

	@Override
	protected IBaseService<CommissionManagement> getBaseService() {
		return this.service;
	}

	/**
	 * 添加
	 * 
	 * @param <V>
	 * @param param
	 * @return
	 */
	@PostMapping("/update")
	public ResponseModel<Integer> update(@Validated @RequestBody CommissionManagementUpdateParam param,
			HttpServletRequest httpServletRequest) {
		int res = this.service.parseUpdate(param, CommissionManagement.class);
		return ResponseModel.<Integer>success(res);
	}

	protected <VO> Class<VO> getTarget() {
		return (Class<VO>) CommissionManagementVO.class;
	}
	
	@Resource
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
}